package com.ruoyi.project.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.PaperQuestionMapper;
import com.ruoyi.project.system.domain.PaperQuestion;
import com.ruoyi.project.system.service.IPaperQuestionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 组卷题Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-11-19
 */
@Service
public class PaperQuestionServiceImpl implements IPaperQuestionService 
{
    @Autowired
    private PaperQuestionMapper paperQuestionMapper;

    /**
     * 查询组卷题
     * 
     * @param id 组卷题ID
     * @return 组卷题
     */
    @Override
    public PaperQuestion selectPaperQuestionById(Long id)
    {
        return paperQuestionMapper.selectPaperQuestionById(id);
    }

    /**
     * 查询组卷题列表
     * 
     * @param paperQuestion 组卷题
     * @return 组卷题
     */
    @Override
    public List<PaperQuestion> selectPaperQuestionList(PaperQuestion paperQuestion)
    {
        return paperQuestionMapper.selectPaperQuestionList(paperQuestion);
    }

    /**
     * 新增组卷题
     * 
     * @param paperQuestion 组卷题
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPaperQuestion(PaperQuestion paperQuestion)
    {
        List<Map<String,String>> list = paperQuestion.getList();
        int row = 0;
        if(list != null){
            for (Map<String,String> resp : list){
               String questionStoreId = resp.get("questionStoreId");
               String score =  resp.get("score");
                PaperQuestion p = new PaperQuestion(){{
                    setPaperId(paperQuestion.getPaperId());
                    setScore(Double.valueOf(score));
                    setQuestionStoreId(Long.valueOf(questionStoreId));
                }};
                row += paperQuestionMapper.insertPaperQuestion(p);
            }
        }
        return row;
    }

    /**
     * 修改组卷题
     * 
     * @param paperQuestion 组卷题
     * @return 结果
     */
    @Override
    public int updatePaperQuestion(PaperQuestion paperQuestion)
    {
        List<Map<String,String>> list = paperQuestion.getList();
        int row = 0;
        if(list != null){
            for (Map<String,String> resp : list){
                String questionStoreId = resp.get("questionStoreId");
                String score =  resp.get("score");
                PaperQuestion p = new PaperQuestion(){{
                    setPaperId(paperQuestion.getPaperId());
                    setScore(Double.valueOf(score));
                    setQuestionStoreId(Long.valueOf(questionStoreId));
                    setId(paperQuestion.getId());
                }};
                row += paperQuestionMapper.updatePaperQuestion(p);
            }
        }
        return row;
    }

    /**
     * 批量删除组卷题
     * 
     * @param ids 需要删除的组卷题ID
     * @return 结果
     */
    @Override
    public int deletePaperQuestionByIds(Long[] ids)
    {
        return paperQuestionMapper.deletePaperQuestionByIds(ids);
    }

    /**
     * 删除组卷题信息
     * 
     * @param id 组卷题ID
     * @return 结果
     */
    @Override
    public int deletePaperQuestionById(Long id)
    {
        return paperQuestionMapper.deletePaperQuestionById(id);
    }
}
